package com.example.administrator.baikangxing.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.view.ColorAnimationView;


public class GuideActivity extends FragmentActivity {

    private static final int[] resource = new int[]{R.drawable.welcome1, R.drawable.welcome2,
            R.drawable.welcome3};
    private static final String TAG = GuideActivity.class.getSimpleName();
    private int guide_state;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉标题栏
        setContentView(R.layout.activity_guide);
        initView();
        //透明状态栏  
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar  
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        /*//透明导航栏  
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);*/
    }

    public void initView() {
        final FrameLayout guide_frame = (FrameLayout) findViewById(R.id.guide_frame);
        MyFragmentStatePager adpter = new MyFragmentStatePager(getSupportFragmentManager());
        ColorAnimationView colorAnimationView = (ColorAnimationView) findViewById(R.id.guide_ColorAnimationView);
        ViewPager viewPager = (ViewPager) findViewById(R.id.guide_viewPager);
        viewPager.setAdapter(adpter);
        colorAnimationView.setmViewPager(viewPager, resource.length);
        colorAnimationView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private TextView textView;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("TAG","onPageScrolled");
            }

            @Override
            public void onPageSelected(int position) {
                Log.e("TAG","onPageSelected");
                if(position == 2) {
                    textView = new TextView(GuideActivity.this);
                    textView.setGravity(Gravity.CENTER);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        textView.setBackground(getResources().getDrawable(R.drawable.edit_bg));
                    }
                    textView.setPadding(20, 10, 20, 10);
                    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(15, 15, 15, 15);
                    textView.setText("进入");
                    textView.setTextColor(getResources().getColor(R.color.orangered));
                    textView.setLayoutParams(lp);
                    guide_frame.addView(textView);
                    textView.setOnClickListener(new View.OnClickListener() {//点击进入
                        @Override
                        public void onClick(View view) {
                            OperateData.saveIntData(GuideActivity.this,"guide_state",1);
                            startActivity(new Intent(GuideActivity.this,LoginActivity.class));
                            finish();
                        }
                    });
                }else {
                    if(textView == null){
                    }else {
                        textView.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.e("TAG","onPageScrollStateChanged");
            }
        });
    }

    public class MyFragmentStatePager
            extends FragmentStatePagerAdapter {

        public MyFragmentStatePager(FragmentManager fm) {
            super(fm);
        }

        @Override public Fragment getItem(int position) {
            return new MyFragment(position);
        }

        @Override public int getCount() {
            return resource.length;
        }
    }

    @SuppressLint("ValidFragment")
    public static class MyFragment extends Fragment {
        private int position;

        public MyFragment(int position) {
            this.position = position;
        }

        @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(resource[position]);
            return imageView;
        }
    }
}
